home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewDisplay-c / Original < prev    next >
Encoding:
Text File  |  1994-12-04  |  10.3 KB  |  380 lines  |  [TEXT/R*ch]

  1. Subject: Display a file program
  2. Message-ID: <1701@uw-beaver>
  3. Date: Wed, 6 Nov 85 22:18:16 MST
  4. Date-Received: Fri, 8 Nov 85 02:05:35 MST
  5. Sender: daemon@uw-beaver
  6. Organization: U of Washington Computer Science
  7. Lines: 372
  8.  
  9. From: kangaro!milo@Purdue.EDU
  10.  
  11. This is a little program I whipped up in Megamax C that will display any
  12. Mac file of type TEXT.  It is very handy for looking at text files you have
  13. downloaded from a BBS without having to load a text editor.  The program
  14. loads in about 4 seconds and supports standard Mac desk accessorys...etc.
  15.  
  16. Let me know if you have any problems with the program...the source follows...
  17.  
  18. Greg Corson
  19. UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  20. ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  21. EDU: kangaro!milo@ee.Purdue.EDU
  22. Or call my BBS at (219) 277-5825
  23.  
  24. /* This sample program allows a user to open a file and print it's contents
  25. ** to a window on the Mac screen.  To give the compiled program an Icon
  26. ** copy the resources from Display.rsrc (use Rmover or Resource Editor) and
  27. ** paste them into the compiled file.  Then use SetFile to change the creator
  28. ** name of the compiled file to DFIL and set the bundle bit.  When you
  29. ** return to the finder the program icon should have an icon.
  30. ** By Greg Corson
  31. ** 19141 Summers Drive
  32. ** South Bend, IN 46637
  33. ** (219) 272-2136
  34. ** UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  35. ** ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  36. ** EDU: kangaro!milo@ee.Purdue.EDU
  37. ** Or call my BBS at (219) 277-5825
  38. */
  39.  
  40. #include <qd.h>
  41. #include <win.h>
  42. #include <menu.h>
  43. #include <event.h>
  44. #include <pack.h>
  45. #include <font.h>
  46.  
  47. #define LASTMENU 4
  48. #define APPLEMENU 1
  49. #define FILEMENU 256
  50. #define EDITMENU 257
  51. #define STOPMENU 258
  52. #define NULL 0L
  53. #define FALSE 0
  54. #define TRUE 1
  55. #define eoferr (-39)
  56.  
  57. rgnhandle    updateregn;
  58. menuhandle   mymenus[LASTMENU+1];
  59. rect         screenrect, dragrect, prect;
  60. boolean      doneflag, temp;
  61. eventrecord  myevent;
  62. int          code, refnum;
  63. windowrecord wrecord;
  64. windowptr    mywindow, whichwindow;
  65. grafptr      temport;
  66. int          themenu, theitem;
  67. int          fileopen,wide,fd1;
  68. long         count;
  69. char         tempbuf[32];
  70.  
  71. main()
  72. {
  73.    #include <qdvars.h>    /* quickdraw globals */
  74.    int i,j;
  75.  
  76. /* Initialize variables */
  77.  
  78.    j = 0;
  79.    doneflag = FALSE;
  80.    fileopen = FALSE;
  81.  
  82. /* Initialize quickdraw, fonts, events, windows, menus, dialogs and cursor */
  83.  
  84.    initgraf(&theport);
  85.    initfonts();
  86.    flushevents(everyevent, 0);
  87.    initwindows();
  88.    initmenus();
  89.    teinit();
  90.    initdialogs(NULL);
  91.    initcursor();
  92.  
  93. /* Create an empty region pointer for use by scrollrect later */
  94.  
  95.    updateregn=newrgn();
  96.  
  97. /* Setup the menu bar */
  98.  
  99.    setupmenus();
  100.  
  101. /* Setup the drag rectangle so part of the window will always be visible */
  102.  
  103.    setrect(&screenrect, 4, 40, 508, 338);
  104.    setrect(&dragrect, 4, 24, screenrect.a.right-4, screenrect.a.bottom-4);
  105.  
  106. /* Create the window and set the current port to the window port */
  107.  
  108.    mywindow = newwindow(&wrecord, &screenrect, "Display a file", TRUE, 0,
  109.              (long)-1, FALSE, (long)0);
  110.    setport(mywindow);
  111.  
  112. /* get the rectangle for the current window and put it in prect */
  113.  
  114.    blockmove(&theport->portrect, &prect, (long)sizeof prect);
  115.    wide = prect.a.right  - prect.a.left;
  116.  
  117. /* Now that the window and menus are drawn set the window font to monaco 9 */
  118.  
  119.    textfont(monaco);
  120.    textsize(9);
  121.    moveto(prect.a.left+1,prect.a.bottom-2);
  122.  
  123. /* Main loop to process events */
  124.  
  125.    do {
  126.  
  127. /**** If a file is open copy a line to the output window */
  128.  
  129.       if(fileopen)
  130.          {
  131.      count=32;
  132.      fsread(fd1, &count, tempbuf);
  133.      if(count == 0)
  134.         {
  135.         fsclose(fd1);
  136.         fileopen=FALSE;
  137.         moveto(prect.a.left+1,prect.a.bottom-2);
  138.         scrollrect(&prect,0,-11,updateregn);
  139.         drawstring("-------End of File-------");
  140.         scrollrect(&prect,0,-11,updateregn);
  141.         moveto(prect.a.left+1,prect.a.bottom-2);
  142.         }
  143.      else
  144.         {
  145.         for(i = 0; i < count; i++)
  146.            {
  147.            if(tempbuf[i] > 31)
  148.               drawchar(tempbuf[i]);
  149.            else
  150.               {
  151. /**************** Scroll window if we get a carriage return */
  152.                   if(tempbuf[i] == '\r')
  153.              {
  154.              j = 0;
  155.              scrollrect(&prect,0,-11,updateregn);
  156.              moveto(prect.a.left+1,prect.a.bottom-2);
  157.              }
  158. /**************** Expand tabs by outputting spaces */
  159.                   if(tempbuf[i] == '\011')
  160.              {
  161.              drawchar(' ');
  162.              j++;
  163.              for(;j & 07;j++)
  164.                 drawchar(' ');
  165.              }
  166.           }
  167.            }
  168.         }
  169.      }
  170.  
  171. /**** Get the next event */
  172.  
  173.       systemtask();
  174.       temp = getnextevent(everyevent, &myevent);
  175.       switch (myevent.what)
  176.          {
  177.      case mousedown:  /* mouse down, call findwindow to figure out where */
  178.         code = findwindow(&myevent.where, &whichwindow);
  179.         switch (code)
  180.            {
  181.            case inmenubar:    /* in meun bar, execute the menu command */
  182.               docommand(menuselect(&myevent.where));
  183.           break;
  184.                case insyswindow:    /* in desk accessory, call desk manager */
  185.               systemclick(&myevent, whichwindow);
  186.           break;
  187.            case indrag:    /* in drag, call dragwindow to move it */
  188.               dragwindow(whichwindow, &myevent.where, &dragrect);
  189.               break;
  190.            case incontent:    /* in content area, make application window the frontmost */
  191.            if (whichwindow != frontwindow())
  192.               selectwindow(whichwindow);
  193.            break;
  194.            }
  195.         break;
  196.      case keydown:    /* If keydown event, check for menu command key */
  197.         if(myevent.modifiers & cmdkey)
  198.            docommand(menukey((char)(myevent.message & 0377)));
  199.         break;
  200.      case autokey:
  201.         break;
  202.      case activateevt:    /* Application window becomming active, do nothing */
  203.         if((myevent.modifiers & 1)&&(((windowptr)myevent.message) == mywindow))
  204.            {
  205.            disableitem(mymenus[3],0);
  206.            enableitem(mymenus[2],0);
  207.            drawmenubar();
  208.            }
  209.         else
  210.            {
  211.            enableitem(mymenus[3],0);
  212.            disableitem(mymenus[2],0);
  213.            drawmenubar();
  214.            }
  215.         break;
  216.      case updateevt:    /* Update event, update the window frame */
  217.         if(((windowptr)myevent.message) == mywindow)
  218.            {
  219.            beginupdate(mywindow);
  220.            endupdate(mywindow);
  221.            }
  222.         break;
  223.     }
  224.     } while (doneflag == 0);
  225. }
  226.  
  227. /*---------------------------------------------------------------------------*/
  228. /* setupmenus()---This subroutine sets up the menu bar and reads in the desk
  229. ** accessory menu
  230. */
  231.  
  232. setupmenus()
  233. {
  234.    int i;
  235.  
  236. /* Apple menu, \024 is the apple character, adresmenu call loads all type DRVR resources */
  237.    mymenus[1] = newmenu(APPLEMENU, "\024");
  238.    appendmenu(mymenus[1], "About \"Display a File\";(-");
  239.    addresmenu(mymenus[1], "DRVR");
  240. /* File menu with open, close and quit selections */
  241.    mymenus[2] = newmenu(FILEMENU, "File");
  242.    appendmenu(mymenus[2], "Open/O;Close/C;Quit/Q");
  243. /* Edit menu with cut, copy and paste */
  244.    mymenus[3] = newmenu(EDITMENU, "Edit");
  245.    appendmenu(mymenus[3], "Undo;(-;Cut;Copy;Paste;Clear;(-;Show Clipboard");
  246. /* Stop scroll menu */
  247.    mymenus[4] = newmenu(STOPMENU,"Click Here to Pause Printout");
  248.    appendmenu(mymenus[4], "Release mouse button to resume printout");
  249.    for (i=1; i<=LASTMENU; i++)
  250.       insertmenu(mymenus[i], 0);
  251. /* Draw the completed menu bar */
  252.    drawmenubar();
  253. }
  254. /*---------------------------------------------------------------------------*/
  255. /* docommand(themenu, theitem)---this subroutine processes commands from the
  256. ** menu bar.  Themenu is the menu ID, theitem is the item number in the menu
  257. */
  258.  
  259. docommand(themenu, theitem)
  260. int themenu, theitem;
  261. {
  262.    char name[256];
  263.    point  openp;
  264.    sfreply rep;
  265.    sftypelist typelist;
  266.    int i;
  267.  
  268. /* Switch to decide what menu the cursor is in */
  269.  
  270.    switch (themenu)
  271.       {
  272.       case APPLEMENU:    /* Mouse down in apple menu */
  273. /******* Item one is the "about Display a file" box */
  274.          if(theitem == 1)
  275.         {
  276.         textfont(systemfont);
  277.         textsize(12);
  278.         eraserect(&prect);
  279.         moveto(prect.a.left,prect.a.top+70);
  280.         center("Display a file program");
  281.         center("Copyright 1985 by Greg Corson");
  282.         center("Kangaroo Koncepts, Inc.");
  283.         center("19141 Summers Drive");
  284.         center("South Bend, IN 46637");
  285.         center("(219) 277-5306");
  286.         textfont(monaco);
  287.             textsize(9);
  288.         move(0,-3);
  289.         center("Feel free to give this program away to all your friends.");
  290.         center("It should NOT be sold for profit.  Be sure to try our");
  291.         center("Computer Based Communications System \"The Connection\"");
  292.         center("Free demo line (219) 277-5825 available 24 hours at 300 or");
  293.         center("1200 baud.  Be sure to look at the \"MacTech\" special");
  294.         center("interest group for information of interest to Mac");
  295.         center("programmers and the \"macintosh\" SIG for general info.");
  296.         pretty();
  297.         moveto(prect.a.left+1,prect.a.bottom-2);
  298.         }
  299. /******* The rest of the items are desk accessorys */
  300.          else
  301.         {
  302.         getitem(mymenus[1], theitem, name);
  303.             refnum = opendeskacc(name);
  304.         setport(mywindow);
  305.         }
  306.      break;
  307.       case FILEMENU:    /* Mouse down in file menu */
  308.          switch(theitem)
  309.         {
  310.         case 1:        /* Open file */
  311.            openp.a.v = 100;
  312.            openp.a.h = 60;
  313.            strncpy(&typelist[0],"TEXT",4);
  314.            sfgetfile(&openp.a,"",NULL,1, typelist, NULL, &rep);
  315.            if(rep.good)
  316.               {
  317.           if(fileopen)
  318.                  {
  319.              fsclose(fd1);
  320.              fileopen = FALSE;
  321.              }
  322.           if(fsopen(rep.fname,rep.vrefnum,&fd1) == noerr)
  323.              {
  324.              scrollrect(&prect,0,-11,updateregn);
  325.              fileopen=TRUE;
  326.              }
  327.           }
  328.            break;
  329.         case 2:        /* Close file */
  330.            if(fileopen)
  331.               {
  332.           fsclose(fd1);
  333.           fileopen = FALSE;
  334.           }
  335.            break;
  336.         case 3:        /* Quit */
  337.            if(fileopen)
  338.               {
  339.           fsclose(fd1);
  340.           fileopen = FALSE;
  341.           }
  342.            doneflag = 1;
  343.            break;
  344.         }
  345.      break;
  346.       case EDITMENU:    /* Process system edit events */
  347.          systemedit(theitem-1);
  348.      break;
  349.    }
  350.    hilitemenu(0);
  351. }
  352. /* Center a string in the window */
  353. center(str)
  354. char   *str;
  355. {
  356.    move(((wide-stringwidth(str))/2), 0);
  357.    drawstring(str);
  358.    move(-(mywindow->pnloc.a.h),(mywindow->txsize)+2);
  359. }
  360. /* draw a pretty design */
  361. pretty()
  362. {
  363.    int j;
  364.    rect tmprec;
  365.  
  366.    blockmove(&prect, &tmprec, (long)sizeof prect);
  367.    for(j=0;j < 12;j++)
  368.       {
  369.       frameoval(&tmprec);
  370.       insetrect(&tmprec,6,0);
  371.       }
  372.    blockmove(&prect, &tmprec, (long)sizeof prect);
  373.    for(j=0;j < 9;j++)
  374.       {
  375.       frameoval(&tmprec);
  376.       insetrect(&tmprec,0,6);
  377.       }
  378. }
  379.